package com.dmyang.p2pchat.thread;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.dmyang.p2pchat.bean.User;
import com.dmyang.p2pchat.comm.constant.GlobalConstant;
import com.dmyang.p2pchat.database.ChatContentDao;
import com.dmyang.p2pchat.database.UserListDao;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Date;
import java.util.List;

/**
 * 用于在后台接收数据的类
 * Created by brss on 2017/11/3.
 */

public class RecieveDataThread extends Thread {

    private Context context;
    private String ip;//本线程监听的
    private int port;
    private UserListDao userListDao;
    private ChatContentDao chatContentDao;

    //传入handler，方便向Activity回传消息
    public RecieveDataThread(Context context, String ip, int port){
        this.context = context;
        this.port = port;
        this.ip = ip;
        userListDao = new UserListDao(context);
        chatContentDao = new ChatContentDao(context);
    }

    @Override
    public void run() {

        //服务器端，实现基于UDP的用户登录
        //1、创建服务器端DatagramSocket，指定端口
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        //2、创建数据报，用于接受客户端发送的数据
        byte[] data = new byte[256];
        while (true) {
            for (int i = 0 ; i < data.length ; i++ ){
                data[i] = 0;
            }
            System.out.println("udp服务器进入死循环中");
            DatagramPacket packet = new DatagramPacket(data, data.length);
            //3、接受客户端发送的数据
            try {
                socket.receive(packet);//此方法在接受数据报之前会一致阻塞
            } catch (IOException e) {
                e.printStackTrace();
            }
            //4、读取数据
            String info = new String(data, 0, data.length);
            info = info.trim();
            System.out.println("我是ip为：" + ip + "手机的服务器，客户端告诉我:" + info);
            String MsgType = info.split(",")[0];
            Message msg = new Message();
            if(MsgType.equals("USERLIST")){
                System.out.println("进入USERLIST");
                String username = info.split(",")[3];
                String ip = info.split(",")[1];
                int port = Integer.parseInt(info.split(",")[2]);
                List list = userListDao.queryByIp(ip);
                if (list.size()==0){
                    userListDao.add(new User(username,ip,port));
                }
            }else if (MsgType.equals("CHATCONTENT")){
                System.out.println("进入CHATCONTENT");
                String nickname = info.split(",")[1];
                String content = info.split(",")[2];
                String ip = packet.getAddress().getHostAddress();
                int port = packet.getPort();
                port=9999;
                String ipAndPort = ip+":"+port;
                com.dmyang.p2pchat.bean.Message message = new com.dmyang.p2pchat.bean.Message(ipAndPort, new Date(), nickname, content);
                chatContentDao.add(message);

            }
            //handler.sendMessage(msg);
        }
    }
}
